<?php
/**
 * Created by PhpStorm.
 * Author: 张志祥
 * QQ: 786811688
 * Phone: 13306281319
 * Date: 2017/3/26
 * Time: 11:08
 */


/**
 * 实例化数据库连接函数
 * @return mysqli
 */
function my_conn()
{
    global $config;
    $my_conn = new mysqli($config['DB_HOST'], $config['DB_USER'], $config['DB_PWD'], $config['DB_NAME'], $config['DB_PORT']);
    if ($my_conn->connect_error) {
        die("连接失败" . $my_conn->conect_error);
    }
    mysqli_set_charset($my_conn, "utf8");// 修改数据库连接字符集为 utf8
    return $my_conn;
}

/**
 *  数据库重连
 * @param $my_conn 需要重新连接的实例
 * @return bool
 */
function mysql_reconnect(&$my_conn)
{
    if ($my_conn->ping()) {
        return true;
    }

    global $config;
    mysqli_real_connect($my_conn, $config['DB_HOST'], $config['DB_USER'], $config['DB_PWD'], $config['DB_NAME'], $config['DB_PORT']);
    if ($error = mysqli_connect_error()) {
        die("连接失败" . $error);
    } else {
        return true;
    }
}

